package cn.tl.hrm.service.impl;

import cn.tl.hrm.constants.CourseConstants;
import cn.tl.hrm.domain.CourseType;
import cn.tl.hrm.mapper.CourseTypeMapper;
import cn.tl.hrm.service.ICourseTypeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author kaizi
 * @since 2022-04-09
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {
    //缓存key
    private static final  String COURSE_TYPE_CACHE_KEY_IN_REDIS = "course_type_cache_key_in_redis";
    @Autowired
    private CourseTypeMapper courseTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    @Cacheable(cacheNames = CourseConstants.KEY_COURSE_TYPE_TRE,key = "'ALL'")
    public List<CourseType> treeData(Long pid) {
        //代码递归
        //return getTreeDataOfRecursion(pid);
        //sql递归 查询n+1次
        //return courseTypeMapper.loadTreeData(pid);
        return getTreeDataOfLoop();


    }

    private List<CourseType> getTreeDataOfLoop() {
        //查询所有的节点
        List<CourseType> courseTypes = courseTypeMapper.selectList(null);
        Map<Long,CourseType> courseTypesMap = new HashMap<>();
        courseTypes.forEach(courseType -> {
            courseTypesMap.put(courseType.getId(),courseType);
        });
        //声明返回顶级节点的List
        List<CourseType> result = new ArrayList<>();
        //遍历所有的节点
       // System.out.println("courseTypes"+courseTypes);
        courseTypes.forEach(courseType -> {
            Long pidTmp = courseType.getPid();
            if (pidTmp.longValue()==0){
                //判断是否是顶级节点,如果是直接放入List
                result.add(courseType);
            }else {
                //如果不是,通过pid获取父亲,进而得到父亲儿子，把自己作为一个儿子放进去
                CourseType parent = courseTypesMap.get(pidTmp);
                //CourseType parent = null;
                //for (CourseType type : courseTypes){
                //  if (type.getId().longValue()==pidTmp.longValue()){
                //      parent=type;
                //      break;
                //      }
                 //}
                parent.getChildren().add(courseType);

            }

        });
        return result;
    }

    private List<CourseType> getTreeDataOfRecursion(Long pid) {
        List<CourseType> courseTypes = courseTypeMapper
                .selectList(new EntityWrapper<CourseType>().eq("pid", pid));
        if (courseTypes==null || courseTypes.size()<1)
            return null;
        for (CourseType type : courseTypes){
            Long pidTmp = type.getId();
            List<CourseType> children = getTreeDataOfRecursion(pidTmp);
            type.setChildren(children);

        }
        return courseTypes;
    }

    @Override
    @CacheEvict(cacheNames = CourseConstants.KEY_COURSE_TYPE_TRE,key = "'ALL'")
    public boolean insert(CourseType entity) {
        super.insert(entity);
        return true;
    }

    @Override
    @CacheEvict(cacheNames = CourseConstants.KEY_COURSE_TYPE_TRE,key = "'ALL'")
    public boolean deleteById(Serializable id) {
        super.deleteById(id);
        return true;
    }

    @Override
    @CacheEvict(cacheNames = CourseConstants.KEY_COURSE_TYPE_TRE,key = "'ALL'")
    public boolean updateById(CourseType entity) {
        super.updateById(entity);
        return true;
    }
}
